package com.yangfei.mystudy1020.FragManagement;


import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yangfei.mystudy1020.R;
import androidx.fragment.app.Fragment;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment1 extends SupportFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    Unbinder unbinder;
    ActivityUpdateViewListener mLis;
    @BindView(R.id.fl_root)
    FrameLayout flRoot;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    int fragCount;


    public Fragment1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

            fragCount = Integer.parseInt(mParam1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        unbinder = ButterKnife.bind(this, view);
        tvTitle.setText("frag" + fragCount);
        int r = (int) (255*(Math.random()));
        int g = (int) (255*(Math.random()));
        int b = (int) (255*(Math.random()));
        flRoot.setBackgroundColor(Color.rgb(r,g,b));
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_title)
    public void onViewClicked() {
        Fragment1 f = Fragment1.newInstance(String.valueOf(++fragCount),null);
        start(f);
        f.initListener(mLis);
        if (mLis != null)
            mLis.updateView();
    }

    public void initListener(ActivityUpdateViewListener lis) {
        mLis = lis;
    }
}
